// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package fr.upstlse.glre.business.service.communiquer;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import fr.upstlse.glre.business.domain.communiquer.Message;
import fr.upstlse.glre.business.domain.communiquer.MessageDao;
import fr.upstlse.glre.business.domain.communiquer.MessageEtat;
import fr.upstlse.glre.business.vo.RFGException;
import fr.upstlse.glre.business.vo.communiquer.BoiteAuxLettresVO;
import fr.upstlse.glre.business.vo.communiquer.MessageVO;

/**
 * @see fr.upstlse.glre.business.service.communiquer.MessageService
 */
public class MessageServiceImpl
    extends fr.upstlse.glre.business.service.communiquer.MessageServiceBase
{
	private static final Logger LOGGER = Logger.getLogger(MessageServiceImpl.class);

    /**
     * @see fr.upstlse.glre.business.service.communiquer.MessageService#findById(long)
     */
    protected fr.upstlse.glre.business.vo.communiquer.MessageVO handleFindById(long pId)
        throws java.lang.Exception
    {
        MessageVO lMessageVO = (MessageVO) getMessageDao().load(MessageDao.TRANSFORM_MESSAGEVO, pId);
        return lMessageVO;
    }

	@Override
	public void envoyer(MessageVO pMessageVO, List pDestinataires) {
		pMessageVO.setEtat(MessageEtat.NOUVEAU);
		super.envoyer(pMessageVO, pDestinataires);
	}

	@Override
	protected void handleEnvoyer(MessageVO pMessageVO, List pDestinataires)
			throws Exception {
		sendAndGet(pMessageVO, pDestinataires);
	}

	@Override
	protected void handleDeleteMessage(MessageVO pMessageVO) throws Exception {
		Message lMessage = getMessageDao().messageVOToEntity(pMessageVO);
		getMessageDao().remove(lMessage);
	}

	@Override
	protected void handleUpdateMessage(MessageVO pMessageVO) throws Exception {
		Message lMessage = getMessageDao().messageVOToEntity(pMessageVO);
		getMessageDao().update(lMessage);
	}

	@Override
	protected void handleRepondre(MessageVO pMessageVO, MessageVO pReponse)
			throws Exception {
		
		List<String> lDestinataires = new ArrayList<String>(1);
		lDestinataires.add(pMessageVO.getExpediteur().getLogin());
		MessageVO lSendedMessage = sendAndGet(pReponse, lDestinataires);
	}

	@Override
	protected MessageVO handleSendAndGet(MessageVO pMessageVO,
			List pDestinataires) throws Exception {
		// Create message
		Message lMessage = getMessageDao().messageVOToEntity(pMessageVO);
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("Message to send id: " + lMessage.getId());
		}
		MessageVO lCreatedMessageVO = (MessageVO) getMessageDao().create(
				MessageDao.TRANSFORM_MESSAGEVO, lMessage);

		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("Send box, receive");
		}
		// Send to receivers mail box.
		for (Object lDestinataire : pDestinataires) {
			BoiteAuxLettresVO lBoiteAuxLettresVO = getBoiteAuxLettresService()
					.findByProprietaireLogin((String) lDestinataire);
			if(lBoiteAuxLettresVO == null) {
				throw new RFGException("The user '" + lDestinataire + "' n'a pas de boite aux lettre");
			}
			getBoiteAuxLettresService().recevoir(lBoiteAuxLettresVO,
					lCreatedMessageVO);
		}
		
		return lCreatedMessageVO;
	}

	@Override
	public MessageVO sendAndGet(MessageVO pMessageVO, List pDestinataires) {
		pMessageVO.setEtat(MessageEtat.NOUVEAU);
		return super.sendAndGet(pMessageVO, pDestinataires);
	}

	@Override
	public void repondre(MessageVO pMessageVO, MessageVO pReponse) {
		pReponse.setEtat(MessageEtat.NOUVEAU);
		super.repondre(pMessageVO, pReponse);
	}

	@Override
	protected MessageVO handleFindFils(MessageVO pParent) throws Exception {
		Message lParentMessage = getMessageDao().messageVOToEntity(pParent);
		MessageVO lFilsMessageVO = (MessageVO) getMessageDao().findFils(MessageDao.TRANSFORM_MESSAGEVO, lParentMessage);
		return lFilsMessageVO;
	}
	
	
	
		
}